package project.view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import project.model.ARoom;
import project.model.Mecung;

public class Map extends JPanel{
	
	private Mecung mecung;
    private GridBagConstraints gridbag;
    private int[][] bando;
    
    public Map(Mecung mecung) {
        this.mecung = mecung;
        this.gridbag = new GridBagConstraints();
        this.bando = mecung.getBanDoThongMinh();

        this.setLayout(new GridBagLayout());
        this.setBackground(Color.BLACK);
    }
    
    public JLabel getBanDoRoom(int i, int j) {
        int index = this.bando[i][j];
        ARoom aroom = this.mecung.getRoomTheoNumber(index);
		JLabel label = new JLabel();
		ImageIcon image = new ImageIcon("./src/project/resource/res_img/wall.jpg");
        if(aroom != null){
        	if(aroom.isMagicRoom()){
        		image = new ImageIcon("./src/project/resource/res_img/magic.jpg");
        	} else {
        		image = new ImageIcon("./src/project/resource/res_img/dungeon.jpg");
        	}
        	
        	if(aroom.isVisitorInRoom()){
                //System.out.println(aroom.getRoomNr());
        		drawHero(i, j);
        	}
        }
        
        Image img = image.getImage();  
        int width = mecung.getChieuRong()/bando[0].length;
        int heigh = mecung.getChieuCao()/bando.length;
        Image newimg = img.getScaledInstance(width, heigh, java.awt.Image.SCALE_SMOOTH); 
        image = new ImageIcon(newimg);
        
        label.setIcon(image);
        return label;
    }

	private void drawHero(int i, int j) {
		JLabel label = new JLabel();
		ImageIcon image = new ImageIcon("./src/project/resource/res_img/hero.png");
        
        Image img = image.getImage();  
        int width = mecung.getChieuRong()/bando[0].length;
        int heigh = mecung.getChieuCao()/bando.length;
        Image newimg = img.getScaledInstance(width, heigh, java.awt.Image.SCALE_SMOOTH); 
        image = new ImageIcon(newimg);
        
        label.setIcon(image);
        
		gridbag.weightx = 1/bando[0].length;
		gridbag.weighty = 1/bando.length;
		gridbag.gridx = j;
		gridbag.gridy = i;
		gridbag.fill = GridBagConstraints.BOTH;
		
		this.add(label, gridbag);
	}

	public void veBanDo() {
        JLabel label = new JLabel();
		
        for (int i = 0; i < bando.length; i++) {
        	for (int j = 0; j < bando[0].length; j++) {
        		label = getBanDoRoom(i, j);
        		gridbag.weightx = 1/bando[i].length;
        		gridbag.weighty = 1/bando.length;
        		gridbag.gridx = j;
        		gridbag.gridy = i;
        		gridbag.fill = GridBagConstraints.BOTH;
        		this.add(label, gridbag);
			}
        }
        
        
    }
}
